package com.etalk.crm.controller;

import com.alibaba.fastjson.JSONObject;
import com.etalk.crm.pojo.NewMainBean;
import com.etalk.crm.service.CCNewMainService;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

import static com.etalk.crm.utils.PubResult.pubResultFunction;

/**
 * @Auther: James
 * @Date: 2018/12/28 09:48
 * @Description: 课程顾问首页工作台
 */
@Controller
@RequestMapping(value = "/CCNewMain",produces = "application/json; charset=utf-8")
public class CCNewMainManageCtl {
    protected static final Logger logger = LogManager.getLogger(CCNewMainManageCtl.class);

    @Resource
    private CCNewMainService ccNewMainService;

    /**
     *首页cc 预约与回访(提醒)
     * @param page
     * @param limit
     * @param searchDate
     * @param session
     * @return
     */
    @RequestMapping(value = "/selectTrackRemindByccId",method = RequestMethod.POST)
    @ResponseBody
    public String selectTrackRemindByccId(Integer ccId,Integer page, Integer limit, String searchDate, HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if(ccId == null){
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.selectTrackRemindByUserId(ccId,searchDate,null,null,page,limit);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("首页cc提醒列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     *根据cc ssc 查询关联学生订单的60天到期信息或课时剩30以下
     * @param page
     * @param limit
     * @param session
     * @return
     */
    @RequestMapping(value = "/searchStuOrderInfoByCC",method = RequestMethod.POST)
    @ResponseBody
    public String searchStuOrderInfoByCC(Integer ccId,Integer page, Integer limit, HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if(ccId == null){
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.searchStuOrderInfoByCC(ccId,null,page,limit);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("首页cc学生订单的60天到期信息或课时剩30以下列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     *课程顾问demo课已约未上学员
     * @param page
     * @param limit
     * @param searchDate
     * @param session
     * @return
     */
    @RequestMapping(value = "/demoMissedListByCC",method = RequestMethod.POST)
    @ResponseBody
    public String demoMissedListByCC(Integer ccId,Integer page, Integer limit, String searchDate, HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if(ccId == null){
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.demoMissedListByCC(ccId,searchDate,page,limit);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("首页cc demo课已约未上学员列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * 查询首页课程顾问下潜在学员
     * @param page
     * @param limit
     * @param session
     * @return
     */
    @RequestMapping(value = "/selectMainTipPotentialByCCId",method = RequestMethod.POST)
    @ResponseBody
    public String selectMainTipPotentialByCCId(Integer ccId,Integer state, Integer page, Integer limit, HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if(ccId == null){
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.selectMainTipPotentialByCCId(ccId,state,page,limit);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("首页cc下潜在学员列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }


        /**
     * 查询首页课程顾问下待约学员
     * @param page
     * @param limit
     * @param session
     * @return
     */
    @RequestMapping(value = "/appointedStudentByccId",method = RequestMethod.POST)
    @ResponseBody
    public String appointedStudentByccId(Integer ccId,Integer state, Integer page, Integer limit, HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if(ccId == null){
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        if(state == null){
            //已上可跟进
            state = 6;
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.selectMainTipPotentialByCCId(ccId,state,page,limit);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("首页cc下待约学员列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }


    /**
     *查询首页渠道为RL(推荐)的所有学员(Ref Leads)
     * @param page
     * @param limit
     * @param session
     * @return
     */
    @RequestMapping(value = "/selectMainTipRelLeadsByCC",method = RequestMethod.POST)
    @ResponseBody
    public String selectMainTipRelLeadsByCC(Integer ccId,Integer page, Integer limit, HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if(ccId == null){
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.selectMainTipRelLeadsByCC(ccId,page,limit);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("首页cc 渠道为RL(推荐)学员列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     *查询首页新学员(第一次购买套餐在一个月31天内)
     * @param page
     * @param limit
     * @param session
     * @return
     */
    @RequestMapping(value = "/selectMainTipNewStudentByCCId",method = RequestMethod.POST)
    @ResponseBody
    public String selectMainTipNewStudentByCCId(Integer ccId,Integer page, Integer limit, HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if(ccId == null){
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.selectMainTipNewStudentByCCId(ccId,page,limit);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("首页cc 新学员列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     *oc课课后回访
     * @param page
     * @param limit
     * @param session
     * @return
     */
    @RequestMapping(value = "/selectOcInfoByCC",method = RequestMethod.POST)
    @ResponseBody
    public String selectOcInfoByCC(Integer ccId,Integer page, Integer limit, HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if(ccId == null){
            ccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ccNewMainService.selectOcInfoByCC(ccId,page,limit);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("首页cc oc课课后回访列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

}
